package com.suning.sawp.in.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.po.customer.BaseTagGrade;
import com.suning.sawp.service.impl.oga.TagAndGradeService;
import com.suning.sawp.service.util.BaseContants;
import com.suning.sawp.service.util.GsonUtils;

/**
 * 〈主推任务后台控制类〉<br>
 * 〈功能详细描述〉
 *
 * @author 12061748
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/adminPage/tagGrade")
public class TagAndGradeController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Autowired
    TagAndGradeService tagAndGradeService;

    /**
     * 
     * 功能描述: 查询标签列表<br>
     * 〈功能详细描述〉
     *
     * @param pageNo
     * @param pageLimit
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping(value = "/query.do", produces = "application/json; charset=utf-8")
    public MyGsonView query(String type) {
        MyGsonView gsonView = createGsonView();
        try {
            List<BaseTagGrade> tagList = tagAndGradeService.queryTagList(type);
            gsonView.addStaticAttribute(BaseContants.DATA, tagList);
        } catch (Exception e) {
            LOGGER.error("TagAndGradeController query type error", e);
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
        }
        return gsonView;
    }

    /**
     * 
     * 功能描述: 保存标签或者评级列表<br>
     * 〈功能详细描述〉
     *
     * @param pageNo
     * @param pageLimit
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping(value = "/upsert.do", produces = "application/json; charset=utf-8")
    public MyGsonView upsert(String json) {
        BaseTagGrade baseTagGrade = GsonUtils.fromJson(json, BaseTagGrade.class);
        MyGsonView gsonView = createGsonView();
        try {
            if (baseTagGrade.getId() == null) {
                if (exsitTagNames("", baseTagGrade.getType()) >= 20) {
                    setErrorMsg(gsonView, ErrorCodeConstants.OUT_GRADE_ERR_E006);
                    return gsonView;
                }
                if (exsitTagNames(baseTagGrade.getName(), baseTagGrade.getType()) > 0) {
                    setErrorMsg(gsonView, ErrorCodeConstants.OUT_GRADE_ERR_E007);
                    return gsonView;
                }
                tagAndGradeService.saveTagOrGrade(baseTagGrade);
            } else {
                tagAndGradeService.updateTagOrGrade(baseTagGrade);
            }
            gsonView.addStaticAttribute(BaseContants.DATA, "");
        } catch (Exception e) {
            LOGGER.error("TagAndGradeController upsert error", e);
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
        }
        return gsonView;
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param baseTagGrade
     * @param gsonView
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private Integer exsitTagNames(String name, String type) {
        if (BaseTagGrade.TypeConstants.TYPE_TAG.equals(type)) {
            return tagAndGradeService.countTagNum(name, type);
        }
        return 0;
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param json
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping(value = "/delete.do", produces = "application/json; charset=utf-8")
    public MyGsonView delete(long id) {
        MyGsonView gsonView = createGsonView();
        try {
            tagAndGradeService.delete(id);
            gsonView.addStaticAttribute(BaseContants.DATA, "");
        } catch (Exception e) {
            LOGGER.error("TagAndGradeController delete error", e);
            setErrorMsg(gsonView, ErrorCodeConstants.ERR_0002);
        }
        return gsonView;
    }
}
